﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.Model;
using PPAP.BO;

namespace PPAP.Web.Part
{
    public partial class SampleCardAdd : PageBase
    {
        TestDepartmentBO tbo = new TestDepartmentBO();
        SamplecardBO sbo = new SamplecardBO();
        LogBO lbo = new LogBO();
        PPARBO pbo = new PPARBO();
        SampleCardTaskBO stbo = new SampleCardTaskBO();
        MailTemplateBO mbo = new MailTemplateBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridView1.DataSource = tbo.GetList("");
                GridView1.DataBind();
                if (Request.QueryString["RID"] != null)
                {
                    DataSet ds = sbo.GetList(" pparid=" + Request.QueryString["RID"].ToString());
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        foreach (GridViewRow item in GridView1.Rows)
                        {
                            DropDownList ddlCon = item.FindControl("DropDownList1") as DropDownList;
                            TextBox txtEnd = item.FindControl("TextBox1") as TextBox;
                            CheckBox cbSe = item.FindControl("cbSe") as CheckBox;
                            //ddlCon.Enabled = txtEnd.Enabled = cbSe.Checked;
                            if (dr["TESTDEPARTMENTID"].ToString() == ddlCon.CssClass)
                            {
                                cbSe.Checked = true;
                                    cbSe.Enabled = false;
                                    cbSe.BorderColor = System.Drawing.Color.CadetBlue;
                                    ddlCon.SelectedValue = dr["DEPTREP"].ToString();
                                    txtEnd.Text = DateTime.Parse(dr["ENDTIMEPLAN"].ToString()).ToString("yyyy-MM-dd");
                                    continue;
                            }
                        }
                    }
                    if (Request.QueryString["id"] != null)
                    {
                        foreach (GridViewRow item in GridView1.Rows)
                        {
                            DropDownList ddlCon = item.FindControl("DropDownList1") as DropDownList;
                            TextBox txtEnd = item.FindControl("TextBox1") as TextBox;
                            CheckBox cbSe = item.FindControl("cbSe") as CheckBox;
                            if (Request.QueryString["id"].ToString() == ddlCon.CssClass)
                            {
                                cbSe.Checked = true;
                                cbSe.Enabled = true;
                                ddlCon.Enabled = true;
                                txtEnd.Enabled = true;
                            }
                            else
                                cbSe.Enabled = false;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Submit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                int num = 0;
                if (Request.QueryString["RID"] != null && Request.QueryString["id"] != null)
                {
                    foreach (GridViewRow item in GridView1.Rows)
                    {
                        Samplecard sa = new Samplecard();
                        DropDownList ddlCon = item.FindControl("DropDownList1") as DropDownList;
                        TextBox txtEnd = item.FindControl("TextBox1") as TextBox;
                        CheckBox cbSe = item.FindControl("cbSe") as CheckBox;
                        if (ddlCon != null && txtEnd != null && cbSe.Checked && cbSe.Enabled)
                        {
                            sa.PPARID = int.Parse(Request.QueryString["RID"].ToString());
                            sa.RESULT = "";
                            sa.STARTTIME = DateTime.Now;
                            sa.STATUS = 0;
                            sa.TESTDEPARTMENTID = int.Parse(ddlCon.CssClass);
                            sa.VERSION = "01";
                            sa.DEPTREP = ddlCon.SelectedValue;
                            sa.ENDTIMEACTUAL = DateTime.Parse("1900-01-01");
                            sa.ENDTIMEPLAN = DateTime.Parse(txtEnd.Text);
                            sa.Internalcomment = "";
                            sa.Reason = "";
                            num += sbo.UpdateDEPTREP(sa);
                        }
                    }

                    if (num > 0)
                    {
                        lbo.WriteLog(CurrentUser.Id, "Update SampleCard", Request.QueryString["RID"].ToString());
                        AlertReflashOpener("Save success");
                    }
                }
                else if (Request.QueryString["RID"] != null)
                {
                    
                    foreach (GridViewRow item in GridView1.Rows)
                    {
                        Samplecard sa = new Samplecard();
                        DropDownList ddlCon = item.FindControl("DropDownList1") as DropDownList;
                        TextBox txtEnd = item.FindControl("TextBox1") as TextBox;
                        CheckBox cbSe = item.FindControl("cbSe") as CheckBox;
                        if (ddlCon != null && txtEnd != null&&cbSe.Checked&&cbSe.Enabled)
                        {
                            int id = 0;
                            sa.PPARID = int.Parse(Request.QueryString["RID"].ToString());
                            sa.RESULT = "";
                            sa.STARTTIME = DateTime.Now;
                            sa.STATUS = 0;
                            sa.TESTDEPARTMENTID = int.Parse(ddlCon.CssClass);
                            sa.VERSION = "01";
                            sa.DEPTREP = ddlCon.SelectedValue;
                            sa.ENDTIMEACTUAL = DateTime.Parse("1900-01-01");
                            sa.ENDTIMEPLAN = DateTime.Parse(txtEnd.Text);
                            sa.Internalcomment = "";
                            sa.Reason = "";
                            id = sbo.Add(sa);
                            AddSampleCardTask("Create sample card", id);
                            num += id;

                             MailTemplate mail = mbo.GetModel(6);
                        DataSet dsmail = mbo.GetEDM6(id.ToString());
                        if (mail != null && dsmail.Tables[0].Rows.Count > 0)
                        {
                            string title = mail.TITLE.Replace("{Part No.}", dsmail.Tables[0].Rows[0]["bdntpartno"].ToString());
                            title = title.Replace("{Part name}", dsmail.Tables[0].Rows[0]["bdntpartname"].ToString());
                            try
                            {
                                base.SendEmailToUser(dsmail.Tables[0].Rows[0]["email"].ToString(), title, mail.CONTENT,null);
                                lbo.WriteLog(CurrentUser.Id, "Send Email:" + dsmail.Tables[0].Rows[0]["email"].ToString(), Request.QueryString["rid"].ToString());

                            }
                            catch (Exception ex)
                            {
                                AlertMessage(ex.Message.ToString());
                                lbo.WriteLog(CurrentUser.Id, "Send Email:" + ex.Message.ToString(), Request.QueryString["rid"].ToString());
                            }
                        }
                        }
                    }
                   
                    if (num > 0)
                    {
                        PPAR pp = pbo.GetModel(int.Parse(Request.QueryString["RID"].ToString()));
                        if (pp != null)
                        {
                            pp.STATUS = 5;
                            pbo.UpdateForSupplier(pp);
                        }
                        lbo.WriteLog(CurrentUser.Id, "Add SampleCard", pp.ID.ToString());
                        AlertReflashOpener("Add success");
                    }
                }
            }
            catch (Exception ex)
            {
                AlertMessage("Err:"+ex.Message.ToString());
            }
        }

        /// <summary>
        /// 选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cbSe_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow item in GridView1.Rows)
            {
                DropDownList ddlCon = item.FindControl("DropDownList1") as DropDownList;
                TextBox txtEnd = item.FindControl("TextBox1") as TextBox;
                CheckBox cbSe = item.FindControl("cbSe") as CheckBox;
                if (cbSe.Enabled)
                    ddlCon.Enabled = txtEnd.Enabled = cbSe.Checked;
                else
                    ddlCon.Enabled = txtEnd.Enabled = false;
            }
        }

        protected void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow item in GridView1.Rows)
            {
                CheckBox cbSe = item.FindControl("cbSe") as CheckBox;
                if (cbSe.Enabled)
                cbSe.Checked = ((CheckBox)sender).Checked;
            }
            cbSe_CheckedChanged(null, null);
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList dt = e.Row.FindControl("DropDownList1") as DropDownList;
                DataSet ds = tbo.GetUserForTest(dt.CssClass);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    dt.Items.Add(new ListItem(dr["username"].ToString(), dr["username"].ToString()));
                }
            }
        }

        /// <summary>
        /// 保存流程记录
        /// </summary>
        /// <param name="Reamrk"></param>
        private void AddSampleCardTask(string Reamrk,int id)
        {
            SampleCardTask task = new SampleCardTask();
            task.CREATETIME = DateTime.Now;
            task.CREATEUSER = CurrentUser.Id;
            task.REAMRK = Reamrk;
            task.SID = id;
            stbo.Add(task);
        }
    }
}